
import TimeUtils from '../../../utils/TimeUtils';
import PageRouter from '../../../utils/PageRouter';
import {$Toptips} from '../../../components/CompRouter';

const storeStateCodes = [
    "2",//已部署
    "1",//审核中
    "0"//部署中
];

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        storeStateArray: ["已部署", "审核中", "部署中"],
        agentData: {agentName: "全部"},
        storeStatus: "已部署",//已部署
        today: TimeUtils.formatTime(),
    },

    bindTimeChange: function (e) {
        this.setData({
            [e.target.id]: e.detail.value
        })
    },

    showPicker: function (e) {
        this.setData({
            storeStatus: this.data.storeStateArray[e.detail.value]
        })
    },

    selectAgent: function () {
        wx.navigateTo({
            url: '../agentManage/agentQurey/agentQurey?pageType=agentSelection'
        });
    },

    //查询按钮
    search_click(e) {
        let errorMessage = _checkForm(e.detail.value);
        if (errorMessage) {
            $Toptips.error({text: errorMessage});
        } else {
            //页面跳转
            let data = e.detail.value;
            data["agentId"] = this.data.agentData["agentId"] ? this.data.agentData["agentId"] : app.globalData.userData.agentId;
            // data["storeStatus"] = this.data.selectStoreState;
            data["storeStatus"] = storeStateCodes[this.data.storeStateArray.indexOf(this.data.storeStatus)];

            // //开始时间、结束时间都可以选填
            // if (!data["startDate"]) {
            //     data["startDate"] = "2017-03-01";
            // }
            // if (!data["endDates"]) {
            //     data["endDates"] = this.data.today;
            // }

            if (data["storeStatus"] === "0") {
                //部署中
                let params = {
                    optionsData: JSON.stringify(data)
                };
                PageRouter.wxNavigateTo(this, './deploying/deploying', false, params);
            } else {
                //已部署、审核中
                let params = {
                    optionsData: JSON.stringify(data)
                };
                PageRouter.wxNavigateTo(this, '../../myMerchant/yesterdayTrad/yesterdayTrad', false, params);
            }

        }
    }

});

function _checkForm(data) {

    // if (data["storeName"]) {
    //     //校验商户名称
    // }
    //
    // if (!data["startDate"]) {
    //     return "请选择开始时间";
    // }
    //
    // if (!data["endDates"]) {
    //     return "请选择结束时间";
    // }

    if (data["startDate"] && data["endDates"] && new Date(data["startDate"].replace(/\-/g, '\/')) > new Date(data["endDates"].replace(/\-/g, '\/'))) {
        return "开始时间不能大于结束时间";
    }
}